(function() {
    'use strict';
    
    // 基础配置参数
    const baseConfig = {
        gifUrl: 'https://www.sakuraidc.cc/JS/xp.gif',
        zIndex: 9999 // 层级
    };
    
    // 响应式配置 - 根据屏幕宽度调整参数
    const responsiveConfig = {
        mobile: { // 手机端 (< 768px)
            snowflakeCount: 8,
            minSpeed: 0.8,
            maxSpeed: 2,
            minSwing: 0.3,
            maxSwing: 1.5,
            minRotationSpeed: 0.3,
            maxRotationSpeed: 1.5
        },
        tablet: { // 平板端 (768px - 1024px)
            snowflakeCount: 12,
            minSpeed: 1,
            maxSpeed: 2.5,
            minSwing: 0.4,
            maxSwing: 1.8,
            minRotationSpeed: 0.4,
            maxRotationSpeed: 1.8
        },
        desktop: { // 桌面端 (> 1024px)
            snowflakeCount: 15,
            minSpeed: 1,
            maxSpeed: 3,
            minSwing: 0.5,
            maxSwing: 2,
            minRotationSpeed: 0.5,
            maxRotationSpeed: 2
        },
        largeDesktop: { // 大屏桌面 (> 1440px)
            snowflakeCount: 20,
            minSpeed: 1.2,
            maxSpeed: 3.5,
            minSwing: 0.6,
            maxSwing: 2.2,
            minRotationSpeed: 0.6,
            maxRotationSpeed: 2.2
        }
    };
    
    // 获取当前配置
    function getCurrentConfig() {
        const width = window.innerWidth;
        const height = window.innerHeight;
        const isPortrait = height > width;
        const screenSize = Math.max(width, height); // 使用较大的尺寸作为判断依据
        
        // 检测是否为移动设备
        const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
        
        let deviceConfig;
        
        if (isMobile) {
            // 移动设备：根据屏幕的最大尺寸判断
            if (screenSize < 768) {
                deviceConfig = responsiveConfig.mobile;
            } else if (screenSize < 1024) {
                deviceConfig = responsiveConfig.tablet;
            } else {
                deviceConfig = responsiveConfig.desktop;
            }
        } else {
            // 桌面设备：使用传统的宽度判断
            if (width < 768) {
                deviceConfig = responsiveConfig.mobile;
            } else if (width < 1024) {
                deviceConfig = responsiveConfig.tablet;
            } else if (width < 1440) {
                deviceConfig = responsiveConfig.desktop;
            } else {
                deviceConfig = responsiveConfig.largeDesktop;
            }
        }
        
        // 竖屏模式下的特殊调整
        if (isPortrait && isMobile) {
            // 竖屏时适当减少雪花数量，提高性能
            deviceConfig = { ...deviceConfig };
            deviceConfig.snowflakeCount = Math.max(Math.floor(deviceConfig.snowflakeCount * 0.8), 5);
        }
        
        return { ...baseConfig, ...deviceConfig };
    }
    
    // 当前使用的配置
    let config = getCurrentConfig();
    
    // 雪花类
    class Snowflake {
        constructor() {
            this.element = document.createElement('img');
            this.element.src = config.gifUrl;
            this.element.style.position = 'fixed';
            this.element.style.pointerEvents = 'none'; // 点击穿透
            this.element.style.zIndex = config.zIndex;
            this.element.style.userSelect = 'none';
            this.element.style.opacity = '0'; // 初始隐藏，避免闪烁
            
            this.isLoaded = false;
            
            this.reset(true); // 初始化时随机分布
            this.setupStyles();
            
            // 监听图片加载完成事件
            this.element.onload = () => {
                this.size = Math.max(this.element.naturalWidth, this.element.naturalHeight);
                this.isLoaded = true;
                
                // 图片加载完成后，如果雪花在顶部上方，调整位置
                if (this.y < 0) {
                    this.y = Math.random() * (window.innerHeight + this.size);
                }
                
                this.setupStyles();
            };
            
            document.body.appendChild(this.element);
        }
        
        reset(isInitial = false) {
            // 重置雪花属性
            this.x = Math.random() * window.innerWidth;
            
            if (isInitial) {
                // 初始化时随机分布在整个屏幕高度
                this.y = Math.random() * window.innerHeight;
            } else {
                // 重置时从顶部开始
                this.y = -100;
            }
            
            this.speed = config.minSpeed + Math.random() * (config.maxSpeed - config.minSpeed);
            this.swingSpeed = 0.01 + Math.random() * 0.02;
            this.swingAmount = config.minSwing + Math.random() * (config.maxSwing - config.minSwing);
            this.swingOffset = Math.random() * Math.PI * 2;
            this.opacity = 0.3 + Math.random() * 0.7;
            this.rotation = 0;
            // 随机旋转方向：50%概率顺时针，50%概率逆时针
            this.rotationSpeed = (config.minRotationSpeed + Math.random() * (config.maxRotationSpeed - config.minRotationSpeed)) * (Math.random() < 0.5 ? 1 : -1);
        }
        
        setupStyles() {
            this.element.style.transform = `translate(${this.x}px, ${this.y}px) rotate(${this.rotation}deg)`;
            this.element.style.opacity = this.isLoaded ? this.opacity : '0';
            this.element.style.left = this.x + 'px';
            this.element.style.top = this.y + 'px';
        }
        
        update() {
            // 正常飘落状态的更新逻辑
            this.y += this.speed;
            
            // 摆动效果
            this.swingOffset += this.swingSpeed;
            this.x += Math.sin(this.swingOffset) * this.swingAmount;
            
            // 旋转效果
            this.rotation += this.rotationSpeed;
            
            // 边界检查
            if (this.y > window.innerHeight + 100) {
                this.reset();
            }
            
            // 水平边界检查
            if (this.x < -100) {
                this.x = window.innerWidth + 100;
            } else if (this.x > window.innerWidth + 100) {
                this.x = -100;
            }
            
            this.setupStyles();
        }
        
        destroy() {
            if (this.element && this.element.parentNode) {
                this.element.parentNode.removeChild(this.element);
            }
        }
    }
    
    // 雪花管理器
    class SnowfallManager {
        constructor() {
            this.snowflakes = [];
            this.isRunning = false;
            this.animationId = null;
            
            // 监听窗口大小变化
            window.addEventListener('resize', () => this.handleResize());
        }
        
        init() {
            // 页面加载完成后自动开始
            if (document.readyState === 'loading') {
                document.addEventListener('DOMContentLoaded', () => this.start());
            } else {
                this.start();
            }
        }
        
        start() {
            if (this.isRunning) return;
            
            this.isRunning = true;
            
            // 创建雪花
            this.createSnowflakes();
            
            // 开始动画循环
            this.animate();
        }
        
        createSnowflakes() {
            // 清除现有雪花
            this.snowflakes.forEach(snowflake => {
                if (snowflake.element && snowflake.element.parentNode) {
                    snowflake.element.parentNode.removeChild(snowflake.element);
                }
            });
            this.snowflakes = [];
            
            // 根据当前配置创建雪花
            for (let i = 0; i < config.snowflakeCount; i++) {
                const snowflake = new Snowflake();
                this.snowflakes.push(snowflake);
            }
        }
        
        animate() {
            if (!this.isRunning) return;
            
            // 更新所有雪花
            this.snowflakes.forEach(snowflake => snowflake.update());
            
            // 继续动画循环
            this.animationId = requestAnimationFrame(() => this.animate());
        }
        
        stop() {
            this.isRunning = false;
            
            if (this.animationId) {
                cancelAnimationFrame(this.animationId);
                this.animationId = null;
            }
            
            // 清除所有雪花元素
            this.snowflakes.forEach(snowflake => {
                if (snowflake.element && snowflake.element.parentNode) {
                    snowflake.element.parentNode.removeChild(snowflake.element);
                }
            });
            this.snowflakes = [];
        }
        
        handleResize() {
            // 更新配置
            const newConfig = getCurrentConfig();
            const needRecreate = newConfig.snowflakeCount !== config.snowflakeCount;
            config = newConfig;
            
            if (needRecreate) {
                // 雪花数量变化，重新创建
                this.createSnowflakes();
            } else {
                // 仅调整现有雪花位置
                this.snowflakes.forEach(snowflake => {
                    const effectiveSize = snowflake.size || 100;
                    if (snowflake.x > window.innerWidth) {
                        snowflake.x = window.innerWidth - effectiveSize;
                    }
                });
            }
        }
    }
    
    // 创建全局实例
    const snowfallManager = new SnowfallManager();
    
    // 暴露控制接口到全局
    window.SnowfallEffect = {
        start: () => snowfallManager.start(),
        stop: () => snowfallManager.stop(),
        config: config
    };
    
    // 自动初始化
    snowfallManager.init();
    
})();

// 使用说明：
// 1. 通过 <script src="snowfall-no-click.js"></script> 引入
// 2. 自动开始雪花效果
// 3. 雪花支持点击穿透，不会阻挡页面交互
// 4. 可通过 SnowfallEffect.stop() 停止效果
// 5. 可通过 SnowfallEffect.start() 重新开始效果